package com.ruoyi.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.management.pojo.energymeter.EnergyMeterDTO;
import com.ruoyi.management.pojo.energymeter.EnergyMeterVO;
import com.ruoyi.management.service.EnergyMeterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电表型号
 */
@Slf4j
@RestController
@RequestMapping("/energyMeter")
public class EnergyMeterController extends BaseController {

    @Autowired
    private EnergyMeterService energyMeterService;

    /**
     * 根据id查询电表型号
     *
     * @param modelId 电表型号id
     * @return 电表型号信息
     */
    @GetMapping("/getEnergyMeterById/{modelId}")
    public R getEnergyMeterById(@PathVariable Long modelId) {
        EnergyMeterVO energyMeterVO = energyMeterService.getEnergyMeterById(modelId);
        return R.ok(energyMeterVO);
    }

    /**
     * 查询电表型号列表
     *
     * @param energyMeterDTO 查询参数
     * @return 电表型号列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EnergyMeterDTO energyMeterDTO) {
        List<EnergyMeterVO> energyMeterVOList = energyMeterService.getEnergyMeterList(energyMeterDTO);
        return getDataTable(energyMeterVOList);
    }

    /**
     * 分页查询电表型号列表
     *
     * @param energyMeterDTO 查询参数
     * @return 电表型号列表
     */
    @GetMapping("/pageList")
    public TableDataInfo pageList(EnergyMeterDTO energyMeterDTO) {
        Page<EnergyMeterVO> energyMeterVOPage = energyMeterService.getEnergyMeterPageList(energyMeterDTO);
        return getDataTable(energyMeterVOPage.getRecords(), "total", String.valueOf(energyMeterVOPage.getTotal()));
    }

    /**
     * 添加电表型号
     *
     * @param energyMeterDTO 电表型号信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public R addEnergyMeter(@RequestBody EnergyMeterDTO energyMeterDTO) {
        EnergyMeterVO energyMeterVO1 = energyMeterService.addEnergyMeter(energyMeterDTO);
        return R.ok(energyMeterVO1);
    }

    /**
     * 修改电表型号
     *
     * @param energyMeterDTO 电表型号信息
     * @return 修改结果
     */
    @PutMapping("/update")
    public R updateEnergyMeter(@RequestBody EnergyMeterDTO energyMeterDTO) {
        int update = energyMeterService.updateEnergyMeter(energyMeterDTO);
        return R.ok(update);
    }

    /**
     * 删除电表型号
     *
     * @param modelId 电表型号id
     * @return 删除结果
     */
    @DeleteMapping("/delete/{modelId}")
    public R deleteEnergyMeter(@PathVariable Long modelId) {
        int delete = energyMeterService.deleteEnergyMeter(modelId);
        return R.ok(delete);
    }
}
